package com.hwadeefsc.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.hwadeefsc.springboot.dao.UsersDao;
import com.hwadeefsc.springboot.dao.MenuDao;
import com.hwadeefsc.springboot.entity.Menu;
import com.hwadeefsc.springboot.entity.Users;
import com.hwadeefsc.springboot.service.LoginService;
import com.hwadeefsc.springboot.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(rollbackOn = Exception.class)
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private  UsersDao userDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public LoginUserVo loginUser(Users user) {
        //用户账号密码验证
        Users loginUser = userDao.getUserByNameAndPassword(user);
        if(null!=loginUser){//验证成功
            LoginUserVo loginUservo = new LoginUserVo();
            loginUservo.setUser(loginUser);
            String token= UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("token::"+token, JSON.toJSONString(loginUser),600, TimeUnit.MINUTES);
            System.out.println(redisTemplate.opsForValue());
            loginUservo.setToken(token);
            List<Menu> menus=menuDao.getMenuByUserType(loginUser);
            loginUservo.setMenus(menus);
            return loginUservo;
        }
        return null;
    }

}
